package io.lkt.modules.sys.controller;

import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.BArticleTempEntity;
import io.lkt.modules.sys.entity.BArticleTempTypeEntity;
import io.lkt.modules.sys.service.BArticleTempService;
import io.lkt.modules.sys.service.BArticleTempTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 文章库模板分类信息表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2021-03-05 10:02:05
 */
@RestController
@RequestMapping("/sys/barticletemptype")
public class BArticleTempTypeController extends AbstractController {
    @Autowired
    private BArticleTempService bArticleTempService;

    @Autowired
    private BArticleTempTypeService bArticleTempTypeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:barticletemptype:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = bArticleTempTypeService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/lists")
//    @RequiresPermissions("sys:barticletemptype:list")
    public R lists() {
        List<BArticleTempTypeEntity> info = bArticleTempTypeService.selectList();

        return R.ok().put("bArticleTempTypeLists", info);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:barticletemptype:info")
    public R info(@PathVariable("id") Integer id) {
        BArticleTempTypeEntity bArticleTempType = bArticleTempTypeService.getById(id);

        return R.ok().put("bArticleTempType", bArticleTempType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:barticletemptype:save")
    public R save(@RequestBody BArticleTempTypeEntity bArticleTempType) {
        if (bArticleTempType == null || bArticleTempType.getTypeName() == null || bArticleTempType.getTypeName().trim().equals("")) {
            return R.error("分类名称不能为空");
        }

        List<BArticleTempTypeEntity> list = bArticleTempTypeService.getByName(bArticleTempType.getTypeName());
        int tall = 0;
        if (list != null && list.size() > 0) {
            tall = list.size();
        }

        if (tall > 0) {
            return R.error("分类已经存在！");
        }

        bArticleTempType.setCreateBy(getUser().getUsername());
        bArticleTempType.setCreateTime(new Date());
        bArticleTempType.setUpdateBy(getUser().getUsername());
        bArticleTempType.setUpdateTime(new Date());

        bArticleTempTypeService.save(bArticleTempType);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:barticletemptype:update")
    public R update(@RequestBody BArticleTempTypeEntity bArticleTempType) {
        ValidatorUtils.validateEntity(bArticleTempType);

        if (bArticleTempType == null || bArticleTempType.getTypeName() == null || bArticleTempType.getTypeName().trim().equals("")) {
            return R.error("分类名称不能为空");
        }

        List<BArticleTempTypeEntity> list = bArticleTempTypeService.getByName(bArticleTempType.getTypeName());
        int tall = 0;
        if (list != null && list.size() > 0) {
            for (BArticleTempTypeEntity info : list) {
                if (info.getTypeName().equals(bArticleTempType.getTypeName()) && info.getId() != bArticleTempType.getId()) {
                    ++tall;
                }
            }
        }

        if (tall > 0) {
            return R.error("分类已经存在！");
        }

        bArticleTempType.setUpdateBy(getUser().getUsername());
        bArticleTempType.setUpdateTime(new Date());

        bArticleTempTypeService.updateById(bArticleTempType);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:barticletemptype:delete")
    public R delete(@RequestBody Integer[] ids) {
//        bArticleTempTypeService.removeByIds(Arrays.asList(ids));

        if (ids == null || ids.length == 0) {
            return R.error("请先选择分类！");
        }

        for (Integer id : ids) {
            List<BArticleTempEntity> info = bArticleTempService.selectList(id);
            if (info.size() > 0) {
                return R.error("该分类下还有模板文章！");
            }

            BArticleTempTypeEntity bArticleTempType = new BArticleTempTypeEntity();

            bArticleTempType.setId(id);
            bArticleTempType.setStatus(Integer.valueOf(0));
            bArticleTempType.setUpdateBy(getUser().getUsername());
            bArticleTempType.setUpdateTime(new Date());

            bArticleTempTypeService.updateById(bArticleTempType);
        }

        return R.ok();
    }

}
